<template>
  <div class="app-container">
    <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="jump">新增</el-button>
    <!-- <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">新增</el-button> -->
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row>
      <el-table-column align="center" label="ID" width="70">
        <template slot-scope="scope">
          <!-- {{ scope.$index }} -->
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="Title" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>
      <el-table-column label="类别" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.leibie }}
        </template>
      </el-table-column>
      <el-table-column label="地址" width="200" align="center">
        <template slot-scope="scope">
          {{ scope.row.address }}
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center">
        <template slot-scope="scope">
          {{ scope.row.desc }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="Actions" width="250">
        <template slot-scope="scope">
          <!-- <el-button v-if="scope.row.edit" type="success" size="small" icon="el-icon-circle-check-outline" @click="confirmEdit(scope.row)">Ok</el-button> -->
          <!-- <el-button v-else type="primary" size="small" icon="el-icon-edit" @click="chengeEdit(scope.row)">表内编辑</el-button> -->
          <el-button type="primary" size="small" @click="handleUpdate(scope.row)">弹窗编辑</el-button>
          <el-button type="primary" size="small" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :title="dialogStatus" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="70px" style="width: 400px; margin-left:50px;">
        <el-form-item label="标题" prop="title">
          <el-input v-model="temp.title"/>
        </el-form-item>
        <el-form-item label="类别" prop="body">
          <el-input v-model="temp.leibie"/>
        </el-form-item>
        <el-form-item label="地址" prop="edit">
          <el-input v-model="temp.address"/>
        </el-form-item>
        <el-form-item label="描述" prop="edit">
          <el-input v-model="temp.desc"/>
        </el-form-item>
        <el-form-item label="文件地址" prop="edit">
          <el-input v-model="temp.files_address"/>
        </el-form-item>
        <el-form-item label="文件地址">
          <el-upload
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleSuccess"
            :file-list="temp.files"
            list-type="picture"
            class="upload-demo"
            action="http://localhost/api/public/index.php/api/files/upload">
            <el-button size="small" type="primary">点击上传</el-button>
            <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">确定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getlinklist, deleteMovie, updateMovie, formstore } from '@/api/form'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      dialogFormVisible: false,
      temp: {
        id: undefined,
        title: '',
        liebie: '',
        desc: '',
        address: '',
        files_address: '',
        files: []

      },
      rules: {
        // edit: [{ required: true, message: 'edit is required', trigger: 'change' }],
        address: [{ required: true, message: 'timestamp is required', trigger: 'blur' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      dialogStatus: ''

    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getlinklist(this.listQuery).then(response => {
        this.list = response.data
        console.log(response.data)
        this.listLoading = false
      })
    },
    jump(){
    //this.$router.push("/cart")
    //传递的参数用{{ $route.query.goodsId }}获取
    this.$router.push({path: '/form/index'})
    //this.$router.go(-2)
    //后退两步
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
     },
     handleSuccess (res, file, fileList) {
       console.log(res.photo)
       this.temp.files_address = res.photo
       console.log(file)
       console.log(fileList)  // 这里可以获得上传成功的相关信息
    },
    handlePreview(file) {
       console.log(file);
    },
    resetTemp() {
      this.temp = {
        id: undefined,
        liebie: '',
        title: '',
        desc: '',
        address: '',
        files_address: '',
        files: []
      }
    },
    chengeEdit(row) {
      row.edit = 1
    },
    cancelEdit(row) {
      // row.body = row.body
      row.edit = 0
      this.$message({
        message: 'The title has been restored to the original value' + row.id,
        type: 'warning'
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          updateMovie(this.temp).then(response => {
            console.log('修改成功')
            this.data = response.data
            this.resetTemp()
            // row = this.data
            console.log(this.data)
            // 确保后台更新后，前端页面同步更新
            for (const v of this.list) {
              if (v.id === this.temp.id) {
                const index = this.list.indexOf(v)
                this.list.splice(index, 1, this.temp)
                break
              }
            }
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '更新成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDelete(row) {
        // this.listLoading = true
        deleteMovie(row.id).then(response => {
        console.log(response.data)
        // 确保后台更新后，前端页面同步更新
        for (const v of this.list) {
          if (v.id === response.data) {
            const index = this.list.indexOf(v)
            this.list.splice(index, 1)
            break
          }
        }
        // location.reload();
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        // this.listLoading = false,
        // location.reload()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          formstore(this.temp).then(() => {
            // this.list.unshift(this.temp)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '创建成功',
              type: 'success',
              duration: 2000
            })
          })
        }
      })
    },
    confirmEdit(row) {
      row.edit = 0
      // row.originalTitle = row.title
      console.log(row)
      updateArticles(row).then(response => {
        console.log('xiugaichengg')
        console.log(response)
      })
      this.$message({
        message: 'The title has been edited' + row.body + row.originalTitle,
        type: 'success'
      })
    }
  }
}
</script>
